#!/usr/bin/env bash
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@/HaLVM-@PACKAGE_VERSION@

CONFIG=${HOME}/.cabal/halvm-config
CABALBIN=${libdir}/bin/cabal
HSC2HS=${libdir}/bin/hsc2hs
ALEX=${libdir}/bin/alex
HAPPY=${libdir}/bin/happy
HADDOCK=${libdir}/bin/haddock

if [ ! -f ${HOME}/.cabal/halvm-config ]; then
  mkdir -p ${HOME}/.cabal
  ${CABALBIN} --config-file=${CONFIG} user-config init
  sed -ie "s!-- compiler:!compiler:!" ${CONFIG}
  sed -ie "s!-- split-objs: False!split-objs: True!" ${CONFIG}
  sed -ie "s!-- ghc-location:!ghc-location: ${bindir}/halvm-ghc!" ${CONFIG}
  sed -ie "s!-- ghc-pkg-location:!ghc-pkg-location: ${bindir}/halvm-ghc-pkg!" ${CONFIG}
  sed -ie "s!-- alex-location:!alex-location: ${ALEX}!" ${CONFIG}
  sed -ie "s!-- happy-location:!happy-location: ${HAPPY}!" ${CONFIG}
  sed -ie "s!-- haddock-location:!haddock-location: ${HADDOCK}!" ${CONFIG}
  sed -ie "s!-- hsc2hs-location:!hsc2hs-location: ${HSC2HS}!" ${CONFIG}
fi

exec ${CABALBIN} --config-file=${CONFIG} ${1+"$@"}

